<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>socket.io</title>
</head>

<body>
  <script src="/socket.io/socket.io.js"></script>
  <script>
    const socket = io("/");
    // 监听与服务端连接成功
    socket.on("connect", () => {
      console.log("与服务端连接成功");
      socket.send("你好，服务端");
    })

    // 监听服务端发送来的消息
    socket.on("message", (data) => {
      console.log(`接收到服务端发送来的消息：${data}`);
    })

    socket.on("disconnect", () => {
      console.log("与服务端断开连接")
    })
  </script>
</body>

</html>